<?php namespace App\Http\Controllers;

use App\Post as Event;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
use Illuminate\Support\Facades\Input;

class EventsController extends Controller {

	/*
	|--------------------------------------------------------------------------
	| Home Controller
	|--------------------------------------------------------------------------
	|
	| This controller renders your application's "dashboard" for users that
	| are authenticated. Of course, you are free to change or remove the
	| controller as you wish. It is just here to get your app started!
	|
	*/

	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		$this->middleware('auth');
	}

	/**
	 * Show the application dashboard to the user.
	 *
	 * @return Response
	 */
	public function index()
	{
		$c_events = Event::where('module_id', '1')->where('date', '>=', new \DateTime('today'))->orderBy('date', 'asc')->get();
		$p_events = Event::where('module_id', '1')->where('date', '<=', new \DateTime('today'))->orderBy('date', 'asc')->get();
		return view('events.index', compact('c_events', 'p_events'));
	}

	public function details($id)
	{
		$event = Event::find($id);
		return view('events.details', compact('event'));
	}

	public function destroy($id)
	{
		Event::find($id)->delete();
		return redirect('events');
	}

	public function create()
	{
		return view('events.create');
	}

	public function store()
	{
		$image = Input::file('image');
		$filename = $image->getClientOriginalName();
		Input::file('image')->move(public_path().'/images/', $filename);

        $input = Request::all();
        $input['image'] = $filename;
        $input['module_id'] = '1';
        $input['user_id'] ='1';

        Event::create($input);

        return redirect('events');
	}
}